<?php

namespace App\Console\Commands;

use App\Constant\Enum;
use App\Models\Admodel;
use App\Models\Adtype;
use App\Models\Setting;
use Illuminate\Console\Command;
use Illuminate\Support\Facades\DB;

class SystemInstallCommand extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'system:install';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = 'system initialization';

    /**
     * Create a new command instance.
     *
     * @return void
     */
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * Execute the console command.
     *
     * @return int
     */
    public function handle()
    {
        DB::beginTransaction();
        try {
            $this->initSettings();
            $this->makeMenu();
            $this->makeAdtypes();
            $this->makeAdmodels();
            DB::commit();
        } catch (\Exception $exception) {
            DB::rollBack();
            dd($exception);
            $this->error($exception->getMessage());
        }
    }


    protected function initSettings()
    {
        // 初始化配置表
        $defaults = Setting::defaults();
        foreach ($defaults as $key => $value) {
            $type = 'string';
            if (is_array($value)) {
                $type = 'json';
                $value = json_encode($value, JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES);
            }

            if (!Setting::query()->where('key', $key)->exists()) {
                Setting::query()->create([
                    'key' => $key,
                    'value' => $value,
                    'type' => $type,
                ]);
            }
        }
        $this->info('>>> 基础配置已初始化.');
    }

    protected function makeMenu()
    {
        $menus = <<<SQL
INSERT INTO `admin_menu` (`id`, `parent_id`, `order`, `title`, `icon`, `uri`, `extension`, `show`, `created_at`, `updated_at`) VALUES (1, 0, 1, 'Index', 'feather icon-bar-chart-2', '/', '', 1, '2024-05-07 07:15:34', '2024-05-10 18:38:29');
INSERT INTO `admin_menu` (`id`, `parent_id`, `order`, `title`, `icon`, `uri`, `extension`, `show`, `created_at`, `updated_at`) VALUES (2, 0, 20, '系统管理', 'feather icon-settings', NULL, '', 1, '2024-05-07 07:15:34', '2024-08-09 15:22:17');
INSERT INTO `admin_menu` (`id`, `parent_id`, `order`, `title`, `icon`, `uri`, `extension`, `show`, `created_at`, `updated_at`) VALUES (3, 2, 21, 'Users', '', 'auth/users', '', 1, '2024-05-07 07:15:34', '2024-08-09 15:22:17');
INSERT INTO `admin_menu` (`id`, `parent_id`, `order`, `title`, `icon`, `uri`, `extension`, `show`, `created_at`, `updated_at`) VALUES (4, 2, 22, 'Roles', '', 'auth/roles', '', 1, '2024-05-07 07:15:34', '2024-08-09 15:22:17');
INSERT INTO `admin_menu` (`id`, `parent_id`, `order`, `title`, `icon`, `uri`, `extension`, `show`, `created_at`, `updated_at`) VALUES (5, 2, 23, 'Permission', '', 'auth/permissions', '', 1, '2024-05-07 07:15:34', '2024-08-09 15:22:17');
INSERT INTO `admin_menu` (`id`, `parent_id`, `order`, `title`, `icon`, `uri`, `extension`, `show`, `created_at`, `updated_at`) VALUES (6, 2, 24, 'Menu', '', 'auth/menu', '', 1, '2024-05-07 07:15:34', '2024-08-09 15:22:17');
INSERT INTO `admin_menu` (`id`, `parent_id`, `order`, `title`, `icon`, `uri`, `extension`, `show`, `created_at`, `updated_at`) VALUES (7, 2, 25, 'Extensions', '', 'auth/extensions', '', 1, '2024-05-07 07:15:34', '2024-08-09 15:22:17');
INSERT INTO `admin_menu` (`id`, `parent_id`, `order`, `title`, `icon`, `uri`, `extension`, `show`, `created_at`, `updated_at`) VALUES (8, 0, 2, '用户管理', 'fa-users', NULL, '', 1, '2024-05-07 07:56:52', '2024-05-07 07:57:01');
INSERT INTO `admin_menu` (`id`, `parent_id`, `order`, `title`, `icon`, `uri`, `extension`, `show`, `created_at`, `updated_at`) VALUES (9, 8, 3, '用户列表', 'fa-user-plus', 'users', '', 1, '2024-05-07 07:57:29', '2024-05-07 08:00:47');
INSERT INTO `admin_menu` (`id`, `parent_id`, `order`, `title`, `icon`, `uri`, `extension`, `show`, `created_at`, `updated_at`) VALUES (10, 8, 6, '用户红包收益', 'fa-yen', 'adrewards', '', 1, '2024-05-07 07:58:29', '2024-08-09 15:22:17');
INSERT INTO `admin_menu` (`id`, `parent_id`, `order`, `title`, `icon`, `uri`, `extension`, `show`, `created_at`, `updated_at`) VALUES (11, 8, 7, '红包收益明细', 'fa-bar-chart-o', 'adreward-details', '', 1, '2024-05-07 07:59:08', '2024-08-09 15:22:17');
INSERT INTO `admin_menu` (`id`, `parent_id`, `order`, `title`, `icon`, `uri`, `extension`, `show`, `created_at`, `updated_at`) VALUES (12, 0, 10, '广告管理', 'fa-buysellads', NULL, '', 1, '2024-05-07 07:59:49', '2024-08-09 15:22:17');
INSERT INTO `admin_menu` (`id`, `parent_id`, `order`, `title`, `icon`, `uri`, `extension`, `show`, `created_at`, `updated_at`) VALUES (13, 12, 11, '广告类型', 'fa-table', 'adtypes', '', 1, '2024-05-07 08:01:32', '2024-08-09 15:22:17');
INSERT INTO `admin_menu` (`id`, `parent_id`, `order`, `title`, `icon`, `uri`, `extension`, `show`, `created_at`, `updated_at`) VALUES (14, 12, 12, '广告列表', 'fa-audio-description', 'admodels', '', 1, '2024-05-07 08:04:01', '2024-08-09 15:22:17');
INSERT INTO `admin_menu` (`id`, `parent_id`, `order`, `title`, `icon`, `uri`, `extension`, `show`, `created_at`, `updated_at`) VALUES (15, 0, 13, '提现管理', 'fa-500px', 'user-withdraws', '', 1, '2024-05-07 08:04:25', '2024-08-09 15:22:17');
INSERT INTO `admin_menu` (`id`, `parent_id`, `order`, `title`, `icon`, `uri`, `extension`, `show`, `created_at`, `updated_at`) VALUES (18, 0, 19, '基础配置', 'fa-gears', 'configs', '', 1, '2024-05-07 08:07:00', '2024-08-09 15:22:17');
INSERT INTO `admin_menu` (`id`, `parent_id`, `order`, `title`, `icon`, `uri`, `extension`, `show`, `created_at`, `updated_at`) VALUES (19, 8, 8, '用户裂变收益', 'fa-wifi', 'commissions', '', 1, '2024-05-09 16:05:43', '2024-08-09 15:22:17');
INSERT INTO `admin_menu` (`id`, `parent_id`, `order`, `title`, `icon`, `uri`, `extension`, `show`, `created_at`, `updated_at`) VALUES (21, 0, 18, '应用资源', 'fa-android', 'apps', '', 1, '2024-05-11 11:15:50', '2024-08-09 15:22:17');
INSERT INTO `admin_menu` (`id`, `parent_id`, `order`, `title`, `icon`, `uri`, `extension`, `show`, `created_at`, `updated_at`) VALUES (22, 8, 9, '用户帐变记录', 'fa-align-left', 'user-wallet-actives', '', 1, '2024-07-09 17:12:34', '2024-08-09 15:22:17');
INSERT INTO `admin_menu` (`id`, `parent_id`, `order`, `title`, `icon`, `uri`, `extension`, `show`, `created_at`, `updated_at`) VALUES (23, 0, 15, '团长管理', 'fa-address-book', 'groups', '', 1, '2024-07-12 14:34:21', '2024-08-09 15:22:17');
INSERT INTO `admin_menu` (`id`, `parent_id`, `order`, `title`, `icon`, `uri`, `extension`, `show`, `created_at`, `updated_at`) VALUES (24, 0, 14, '团长提现', 'fa-adn', 'group-withdraws', '', 1, '2024-07-12 14:34:45', '2024-08-09 15:22:17');
INSERT INTO `admin_menu` (`id`, `parent_id`, `order`, `title`, `icon`, `uri`, `extension`, `show`, `created_at`, `updated_at`) VALUES (25, 0, 17, '订单列表', 'fa-bar-chart-o', 'orders', '', 1, '2024-07-18 11:47:15', '2024-08-09 15:22:17');
INSERT INTO `admin_menu` (`id`, `parent_id`, `order`, `title`, `icon`, `uri`, `extension`, `show`, `created_at`, `updated_at`) VALUES (26, 0, 16, '团长申请', 'fa-bookmark', 'group-applies', '', 1, '2024-07-18 16:50:32', '2024-08-09 15:22:17');
INSERT INTO `admin_menu` (`id`, `parent_id`, `order`, `title`, `icon`, `uri`, `extension`, `show`, `created_at`, `updated_at`) VALUES (27, 8, 4, '异常用户', 'fa-ban', 'unusual-users', '', 1, '2024-07-19 15:40:42', '2024-07-19 15:59:18');
INSERT INTO `admin_menu` (`id`, `parent_id`, `order`, `title`, `icon`, `uri`, `extension`, `show`, `created_at`, `updated_at`) VALUES (28, 8, 5, '封禁历史', 'fa-ban', 'blocked-users', '', 1, '2024-08-09 15:22:06', '2024-08-09 15:22:17');
SQL;


        DB::table('admin_menu')->truncate();
        DB::unprepared($menus);

        $this->info(">>> 系统菜单已设置.");

    }

    private function makeAdtypes()
    {
        if (Adtype::query()->count() > 0) {
            $this->info("> 已存在广告类型数据，跳过创建.");
            return;
        }
        $adtypes = [
            ['name' => '短视频广告', 'position' => 'shorts', 'reward_rate' => 0.3],
            ['name' => '激励视频广告', 'position' => 'stimulate', 'reward_rate' => 0.3],
            ['name' => 'Banner广告', 'position' => 'banner', 'reward_rate' => 0.3],
            ['name' => '信息流广告', 'position' => 'stream', 'reward_rate' => 0.3],
            ['name' => '插屏广告', 'position' => 'interrupt', 'reward_rate' => 0.3],
            ['name' => '开屏广告', 'position' => 'openscene', 'reward_rate' => 0.3],
        ];

        foreach ($adtypes as $adtype) {
            if (!Adtype::query()->where(['position' => $adtype['position']])->exists()) {
                Adtype::query()->create($adtype);
            }
        }

        $this->info('>>> 广告类型已设置.');
    }

    private function makeAdmodels()
    {
        if (Admodel::query()->count() > 0) {
            $this->info("> 已存在广告模型数据，跳过创建.");
            return;
        }
        $adtypes = Adtype::all();
        $types = [
            Enum::ADTYPE_POSITION_BANNER => '1787399946719215715',
            Enum::ADTYPE_POSITION_INTERRUPT => '1787399816683208734',
            Enum::ADTYPE_POSITION_OPENSCENE => '1787399759405793373',
            Enum::ADTYPE_POSITION_SHORTS => '1787400097923874881',
            Enum::ADTYPE_POSITION_STIMULATE => '1787399858139709491',
            Enum::ADTYPE_POSITION_STREAM => '1787399900552511490',
        ];
        foreach ($adtypes as $adtype) {
            $attributes = [
                'adtype_id' => $adtype->id,
                'value' => $types[$adtype->position],
            ];
            if (!Admodel::query()->where($attributes)->exists()) {
                Admodel::query()->create($attributes);
            }
        }

        $this->info('>>> 广告数据已设置.');
    }
}
